<template>
  <div>
    <el-dialog title="选择位置" :visible.sync="showBuild" :close-on-click-modal="false" width="1000px" append-to-body>
      <el-empty v-if="!buildings.length" />
      <div v-else class="buildings">
        <div v-for="(item1, idx1) in buildings" :key="idx1">
          <div class="title">{{ item1.name }}</div>
          <div class="list">
            <div class="item" v-for="(item2, idx2) in item1.list" :key="idx1 + '_' + idx2" :title="item2.fnameL2" @click="onItem(item2)">
              <svg-icon class="icon" :icon-class="item1.icon" color="#74a8f8" />
              <div class="name ellipsis">{{ item2.fnameL2 }}</div>
            </div>
          </div>
        </div>
      </div>
    </el-dialog>
    <el-dialog title="绘制区域" :visible.sync="showDraw" :close-on-click-modal="false" width="60%" append-to-body>
      <canvasDraw v-if="showDraw" :groups="drawGroups" @save="onDrawSave" @colse="onDrawClose" />
    </el-dialog>
  </div>
</template>
<script>
// 过程检查-选择公区。仅选择公区时使用
import { getCqBuildingList } from '@/api/projectInspection/courseCheck'
import canvasDraw from './canvasDraw.vue'
export default {
  name: 'SelectPublicArea',
  components: { canvasDraw },
  props: { value: { type: Boolean, default: false } },
  data() {
    return {
      rdcScore: {},
      currArea: {},
      showBuild: false,
      buildingLoading: false,
      buildings: [],
      selectBuilding: {},
      showDraw: false,
      drawGroups: [] // 当前绘点数据
    }
  },
  computed: {
    multiDialog() {
      const { showBuild, showDraw } = this
      return { showBuild, showDraw }
    }
  },
  watch: {
    multiDialog({ showBuild, showDraw }) {
      if (!showBuild && !showDraw) this.reset()
    }
  },
  created() {
    this.initData = JSON.parse(JSON.stringify(this.$data))
  },
  methods: {
    reset() {
      setTimeout(() => {
        const data = JSON.parse(JSON.stringify(this.initData))
        Object.keys(data).map(k => (this[k] = data[k]))
      }, 500)
    },
    open(rdcScore, currArea) {
      if (!currArea.fileId) return this.$message.error('暂无图片数据')
      this.rdcScore = rdcScore
      this.currArea = currArea
      this.getBuildings()
      this.showBuild = true
    },
    async getBuildings() {
      const { projectId, randomId } = this.rdcScore
      this.buildingLoading = true
      const res = await getCqBuildingList({ projectId, randomId, buildType: 3 }).catch(() => false)
      this.buildingLoading = false
      if (res === false) return (this.buildings = [])

      const data = res.data || []
      const obj = {
        1: { buildType: 1, name: '景观', list: [], icon: 'scenery' },
        2: { buildType: 2, name: '停车场', list: [], icon: 'park' },
        3: { buildType: 3, name: '地下室', list: [], icon: 'under' }
        // 4: { buildType: 4, name: '桩基', list: [], icon: 'pile' }
      }
      data.map(i => obj[i.publicareatype] && obj[i.publicareatype].list.push(i))
      this.buildings = Object.values(obj).filter(i => i.list.length)
    },
    onItem(item) {
      const { rdcScore, currArea } = this
      // 地下室和防火分区
      if (item.publicareatype === 3) {
        const position = {
          projectId: rdcScore.projectId,
          buildingId: item.fid,
          taskFileId: currArea.taskFileId,
          fileId: currArea.fileId,
          shortName: item.fnameL2
        }
        this.$emit('ok', {
          buildType: item.publicareatype,
          parentId: item.parentId,
          position
        })
        this.showBuild = false
        return
      }

      this.selectBuilding = item
      this.drawGroups = [currArea.drawArea || { fileId: currArea.fileId, scale: '', value: [] }]
      this.showDraw = true
      this.showBuild = false
    },
    onDrawSave(data) {
      const { rdcScore, currArea, selectBuilding } = this
      const position = {
        projectId: rdcScore.projectId,
        buildingId: selectBuilding.fid,
        taskFileId: currArea.taskFileId,
        fileId: currArea.fileId,
        shortName: selectBuilding.fnameL2,
        drawArea: data.groupList[0]
      }
      this.$emit('ok', {
        buildType: selectBuilding.publicareatype,
        position
      })
      this.showDraw = false
    },
    onDrawClose(val) {
      if (!val) return
      this.drawGroups = []
      this.showDraw = false
      // this.showBuild = true
    }
  }
}
</script>
<style lang="scss" scoped>
.buildings {
  .title {
    color: #333;
    font-weight: bold;
    margin: 10px 0;
  }
  .list {
    display: flex;
    flex-wrap: wrap;
  }
  .item {
    margin: 0 15px 15px 0;
    width: 70px;
    text-align: center;
    cursor: pointer;
  }
  .icon {
    padding: 10px;
    border-radius: 6px;
    font-size: 70px;
    background-color: #e0eefd;
    margin-bottom: 5px;
  }
  .name {
    font-size: 12px;
  }
}
</style>
